package com.aliengen.thingple.beep;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.aliengen.thingple.driverapp.R;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public class DevBeep {
    private static SoundPool soundPool = null;
    private static int sound_errID = 0;
    private static int sound_okID = 0;
    private static int stream_beepID=0;
    private static boolean bRunning;
    public static void init(Context ct)
    {
        bRunning = false;
        //if(soundPool == null)
        {
            soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
            //sound_okID = soundPool.load(ct, 0x7f050001, 1);
            //sound_errID = soundPool.load(ct, 0x7f050000, 1);
            sound_okID = soundPool.load(ct, R.raw.beep_ok, 1);
            sound_errID = soundPool.load(ct, R.raw.beep_err, 1);
        }
    }

    public static void release()
    {
        if(stream_beepID >0)soundPool.stop(stream_beepID);
        soundPool.unload(sound_errID);
        soundPool.unload(sound_okID);
        soundPool.release();;
    }

    public static void PlayOK()
    {
        if(bRunning)return;
        if(soundPool==null)return;
        bRunning = true;
        if(stream_beepID >0)soundPool.stop(stream_beepID);
        stream_beepID = soundPool.play(sound_okID , 1.0f, 1.0f, 0, 0, 1.0f);
        bRunning = false;
    }

    public static void PlayErr()
    {
        if(bRunning)return;
        if(soundPool==null)return;
        bRunning = true;
        if(stream_beepID >0)soundPool.stop(stream_beepID);
        stream_beepID = soundPool.play(sound_errID , 1.0f, 1.0f, 0, 0, 1.0f);
        bRunning = false;
    }
}
